﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using FineUI;
using ZHDJ.WebPage.ControlEx;

namespace ZHDJ.WebPage.Picker
{
    /// <summary>
    /// 使用Picker的页面基类
    /// </summary>
    public class UsePickerPageBase: DBPageBase
    {
        
        /// <summary>
        /// 是否启用picker
        /// </summary>
        public virtual bool IsEnabledPicker
        {
            get { return true; }
            
        }

        /// <summary>
        /// 页面上的picker集合
        /// </summary>
        private List<Control> Pickercontrollist
        {
            get; set;
        }

        /// <summary>
        /// 页面上的picker集合
        /// </summary>
        private List<Control> ImagePreviewcontrollist
        {
            get; set;
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
           
            Page control = HttpContext.Current.CurrentHandler as Page;
            Pickercontrollist = new ControlBusiness().FindControl(control, typeof(TriggerBoxPicker));
            ImagePreviewcontrollist = new ControlBusiness().FindControl(control, typeof(HyperLinkImagePreview));
            foreach (var item in Pickercontrollist)
            {
                var picker = item as TriggerBoxPicker;
                if (!(ControlUtil.FindControl(picker.ID + "window") is Window))
                {
                    var window1 = new Window
                    {
                        ID = picker.ID + "window",
                        Title = "编辑",
                        Hidden = true,
                        EnableIFrame = true,
                        EnableMaximize = true,
                        EnableResize = true,
                        Target = Target.Top,
                        IsModal = true,
                        Width = picker.WindinsWidth,
                        Height = picker.WindinsHeight
                    };
                    HiddenField hf = new HiddenField() {ID = picker.ID + "hidden"};
                    //为页面增加窗体
                    this.Form.Controls.Add(window1);
                    //为页面增加隐藏域
                    this.Form.Controls.Add(hf);
                }
            }
            foreach (var item in ImagePreviewcontrollist)
            {
                var picker = item as HyperLinkImagePreview;
                if (!(ControlUtil.FindControl("windowImagePreview") is Window))
                {
                    var window1 = new Window
                    {
                        ID = "windowImagePreview",
                        Title = "图片预览",
                        Hidden = true,
                        EnableIFrame = true,
                        EnableMaximize = true,
                        EnableResize = true,
                        Target = Target.Parent,
                        IsModal = true,
                        Width = picker.WindinsWidth,
                        Height = picker.WindinsHeight,
                        
                    };
                    //为页面增加窗体
                    this.Form.Controls.Add(window1);
                }
            }
        }
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
           
          
            //增加picker的弹框事件
            foreach (var item in Pickercontrollist)
            {
               
                var picker = item as TriggerBoxPicker;
                if (!picker.EnablePicker)
                {
                    picker.AutoPostBack = false;
                    picker.OnClientTrigger2Click = ";return false;";
                    continue;
                }
                var window = ControlUtil.FindControl(picker.ID + "window") as Window;
                var hiddenField = ControlUtil.FindControl(picker.ID + "hidden") as HiddenField;
                var pickerurl = String.Format(picker.WindinsUrl + 
                    (picker.WindinsUrl.IndexOf("?") > 0 ? "&" : "?") + "ids=<script>{0}</script>", 
                    hiddenField.GetValueReference());
                picker.OnClientTrigger2Click = window.GetSaveStateReference(hiddenField.ClientID, picker.ClientID)
                    + window.GetShowReference(pickerurl, picker.WindinsTitle);
               picker.OnClientTrigger1Click = picker.ClearValueJS();
                
            }
            foreach (var item in ImagePreviewcontrollist)
            {
                var imageP = item as HyperLinkImagePreview;
                var window = ControlUtil.FindControl("windowImagePreview") as Window;
                if (window != null)
                    imageP.OnClientClick = window.GetShowReference(imageP.WindinsUrl + "?id=" + imageP.ImageID, "图片预览");
            }
        }

    }
}
